package cn.kgc.ssm.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.ssm.dto.CoursePageDTO;
import cn.kgc.ssm.entity.Course;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.CourseService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/11/21
 * @description  课程管理
 */
@RestController
@RequestMapping("course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 分页
    @GetMapping("page")
    public ResponseResult coursePage(CoursePageDTO coursePageDTO){

       return courseService.coursePage(coursePageDTO);

    }

    // 课程图片上传  upload
    @PostMapping("upload")
    public ResponseResult uploadCourseImg(MultipartFile file){

        String originalFilename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        String uuid = IdUtil.simpleUUID();
        String lastName = uuid+"."+filenameExtension;

        File f = new File("D:/upload/course/" + lastName);
        try {
            file.transferTo(f);
            return  ResponseResult.success().data(lastName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    @PostMapping("uploadInfo")
    public ResponseResult  addUploadInfo(Course course){

       return courseService.addCoure(course);
    }


    // 获取课程图片  getCoursePicture
    @GetMapping("getCoursePicture")
    public void  getCoursePicture(String img, HttpServletResponse response){

        try {
            FileInputStream fileInputStream = new FileInputStream("D:/upload/course/" + img);
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtils.copy(fileInputStream,outputStream);
            fileInputStream.close();
            outputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("update")
    public ResponseResult  updateCourse(Course course){

        return courseService.updateCoure(course);
    }


    // 更新课程图片
    @PostMapping("updateImg")
    public ResponseResult editCourseImg(String courseId,String fileName,String img){
        return  courseService.editCourseImg(courseId,fileName,img);
    }


    @PostMapping("delete")
    public ResponseResult deleteCourse(String img){
        return  courseService.deleteCourse(img);
    }


}
